package com.hk.crm.workbench.web.controller;

import com.hk.crm.VO.PaginationVO;
import com.hk.crm.settings.domain.User;
import com.hk.crm.settings.service.UserService;
import com.hk.crm.settings.service.impl.UserServiceImpl;
import com.hk.crm.utils.DateTimeUtil;
import com.hk.crm.utils.PrintJson;
import com.hk.crm.utils.ServiceFactory;
import com.hk.crm.utils.UUIDUtil;
import com.hk.crm.workbench.domain.Activity;
import com.hk.crm.workbench.domain.ActivityRemark;
import com.hk.crm.workbench.domain.Clue;
import com.hk.crm.workbench.service.ActivityService;
import com.hk.crm.workbench.service.impl.ActivityServiceImpl;
import com.mysql.jdbc.TimeUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class ActivityController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入市场活动控制器");
        String path=req.getServletPath();
        if ("/workbench/activity/getUserList.do".equals(path)){
            getUserList(req,resp);
        }else if("/workbench/activity/save.do".equals(path)){
            save(req,resp);
        }else if("/workbench/activity/pageList.do".equals(path)){
            pageList(req,resp);
        }else if("/workbench/activity/delete.do".equals(path)){
            delete(req,resp);
        }else if("/workbench/activity/getUserListAndActivity.do".equals(path)){
            getUserListAndActivity(req,resp);
        }else if("/workbench/activity/update.do".equals(path)){
            update(req,resp);
        }else if("/workbench/activity/detail.do".equals(path)){
            detail(req,resp);
        }
        else if("/workbench/activity/getRemarkListByAid.do".equals(path)){
            getRemarkListByAid(req,resp);
        }else if("/workbench/activity/deleteRemark.do".equals(path)){
            deleteRemark(req,resp);
        }else if("/workbench/activity/saveRemark.do".equals(path)){
            saveRemark(req,resp);
        }else if("/workbench/activity/updateRemark.do".equals(path)){
            updateRemark(req,resp);
        }

    }

    private void updateRemark(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("执行修改备注操作");
        ActivityService proxyService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        String id=req.getParameter("id");
        String noteContent=req.getParameter("noteContent");
        String editTime=DateTimeUtil.getSysTime();
        String editBy=((User) req.getSession().getAttribute("user")).getNAME();
        String editFlag="1";
        ActivityRemark ar=new ActivityRemark();
        ar.setId(id);
        ar.setNoteContent(noteContent);
        ar.setEditFlag(editFlag);
        ar.setEditBy(editBy);
        ar.setEditTime(editTime);
        boolean flag=proxyService.updateRemark(ar);
//        前端要success和ar  就用map打包 转换json串传给前端
        Map<String,Object> map=new HashMap<>();
        map.put("success", flag);
        map.put("ar", ar);
        PrintJson.printJsonObj(resp, map);
    }

    private void saveRemark(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("执行添加备注操作");
        String noteContent=req.getParameter("noteContent");
        String activityId=req.getParameter("activityId");
        String id=UUIDUtil.getUUID();
        String createTime=DateTimeUtil.getSysTime();
        String createBy=((User) req.getSession().getAttribute("user")).getNAME();
        String editFlag="0";
        ActivityRemark ar=new ActivityRemark();
        ar.setNoteContent(noteContent);
        ar.setActivityId(activityId);
        ar.setId(id);
        ar.setCreateBy(createBy);
        ar.setCreateTime(createTime);
        ar.setEditFlag(editFlag);
        ActivityService proxyService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        boolean flag=proxyService.saveRemark(ar);
        Map<String,Object> map=new HashMap<>();
        map.put("success", flag);
        map.put("ar", ar);
        PrintJson.printJsonObj(resp, map);
    }

    private void deleteRemark(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("删除备注操作");
        String id=req.getParameter("id");
        ActivityService proxyService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        boolean flag=proxyService.deleteRemark(id);
        PrintJson.printJsonFlag(resp, flag);
    }

    private void getRemarkListByAid(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("根据市场活动id取得备注列表");
        String activityId=req.getParameter("activityId");
        ActivityService proxyService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        List<ActivityRemark> arList=proxyService.getRemarkListByAid(activityId);
        PrintJson.printJsonObj(resp, arList);
    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入到跳转信息页的操作");
        String id=req.getParameter("id");
        ActivityService proxyService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        Activity a=proxyService.detail(id);
//        将得到的a存到request域
        req.setAttribute("a", a);

        req.getRequestDispatcher("/workbench/activity/detail.jsp").forward(req, resp);

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        //        接收参数
        String id= req.getParameter("id");
        String owner=req.getParameter("owner");
        String name=req.getParameter("name");
        String startDate=req.getParameter("startDate");
        String endDate=req.getParameter("endDate");
        String cost=req.getParameter("cost");
        String description=req.getParameter("description");
//        修改时间：当前系统时间
        String editTime= DateTimeUtil.getSysTime();
        //        修改人：当前登录的用户
        String editBy=((User)req.getSession().getAttribute("user")).getNAME();
//        将上述的数据封装到一个对象中传入方法
        Activity activity=new Activity();
        activity.setId(id);
        activity.setOwner(owner);
        activity.setName(name);
        activity.setStartDate(startDate);
        activity.setEndDate(endDate);
        activity.setCost(cost);
        activity.setDescription(description);
        activity.setEditTime(editTime);
        activity.setEditBy(editBy);

//        获取市场活动 业务层的代理类
        ActivityService proxyService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
//        返回布尔值表示修改成功还是失败
        boolean flag=proxyService.update(activity);
//       调用PrintJson的方法将返回的布尔值转为json串传给前端
        PrintJson.printJsonFlag(resp, flag);

    }

    private void getUserListAndActivity(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("进入到查询用户信息列表和根据市场活动查询单条记录的操作");
        String id=req.getParameter("id");
        ActivityService proxyService= (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
//        返回值有个ulist和市场活动对象a，用map返回
/*
*       总结：
*           controller调用service的方法返回值应该是什么得想一下前端要什么，就要从service层取什么
*           前端需要的，管业务层去要，以上两个信息，复用率不高，我们选择使用map打包这个信息即可
*
* */
        Map<String,Object> map=proxyService.getUserListAndActivity(id);
        PrintJson.printJsonObj(resp, map);
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("执行市场活动的删除操作");
        String ids[]=req.getParameterValues("id");
        ActivityService proxyService= (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        boolean flag=proxyService.delete(ids);
        PrintJson.printJsonFlag(resp, flag);
    }

    private void pageList(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("进入到查询市场活动信息列表的操作（结合条件查询和分页查询）");
        String name=req.getParameter("name");
        String owner=req.getParameter("owner");
        String startDate=req.getParameter("startDate");
        String endDate=req.getParameter("endDate");
//        代表第几页
        String pageNoStr=req.getParameter("pageNo");
        int pageNo=Integer.valueOf(pageNoStr);
//       代表每页展现几条数据
        String pageSizeStr=req.getParameter("pageSize");
        int pageSize=Integer.valueOf(pageSizeStr);
//        计算出略过的记录数
        int skipCount=(pageNo-1)*pageSize;
        Map<String ,Object> map=new HashMap();
        map.put("name",name);
        map.put("owner", owner);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("skipCount", skipCount);
        map.put("pageSize", pageSize);
        ActivityService proxyService= (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
/*
            前端要：市场活动信息列表
            查询的总条数
            业务层拿到了上述两项信息之后如何做返回
                map
                vo
* */
//        Service返回的是个VO，里面是从数据库得到的List<Activity> 和total 数据总数
        PaginationVO<Activity> vo=proxyService.pageList(map);
//        将得到的VO转成json串传给前端
        PrintJson.printJsonObj(resp, vo);

    }

    private void save(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("执行市场活动的添加操作");
//        接收参数
        String id= UUIDUtil.getUUID();
        String owner=req.getParameter("owner");
        String name=req.getParameter("name");
        String startDate=req.getParameter("startDate");
        String endDate=req.getParameter("endDate");
        String cost=req.getParameter("cost");
        String description=req.getParameter("description");
//        创建时间：当前系统时间
        String createTime= DateTimeUtil.getSysTime();
        //        创建人：当前登录的用户
        String createBy=((User)req.getSession().getAttribute("user")).getNAME();
//        将上述的数据封装到一个对象中传入方法
        Activity activity=new Activity();
        activity.setId(id);
        activity.setOwner(owner);
        activity.setName(name);
        activity.setStartDate(startDate);
        activity.setEndDate(endDate);
        activity.setCost(cost);
        activity.setDescription(description);
        activity.setCreateTime(createTime);
        activity.setCreateBy(createBy);
//        获取市场活动 业务层的代理类
        ActivityService proxyService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        boolean flag=proxyService.save(activity);
//       调用PrintJson的方法将返回的布尔值转为json串传给前端
        PrintJson.printJsonFlag(resp, flag);

    }

    private void getUserList(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("取得用户信息列表");
        UserService proxyService= (UserService) ServiceFactory.getService(new UserServiceImpl());
        List<User> userList=proxyService.getUserList();
        PrintJson.printJsonObj(resp, userList);

    }
}
